package com.zjw.zy.ext

import android.content.Context
import android.view.MotionEvent
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.FrameLayout
import android.widget.LinearLayout
import androidx.annotation.IdRes
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.constraintlayout.widget.ConstraintLayout.LayoutParams.PARENT_ID
import androidx.constraintlayout.widget.ConstraintSet
import com.blankj.utilcode.util.Utils
import com.zjw.zy.base.BaseActivity
import com.zjw.zy.utils.ViewUtils
import java.lang.Long.max


fun View.focus() {
    this.isFocusable = true
    this.isFocusableInTouchMode = true
    this.requestFocus()
}


fun View.unFocus() {
    this.isFocusable = false
    this.isFocusableInTouchMode = false
}

fun View.byHint(event: MotionEvent): Boolean {
    return ViewUtils.hintView(this, event)
}

fun View.clickInterval(interval: Long) {
    return ViewUtils.hookClickInterval(this, interval)
}

fun View.setOnClickListener(listener: View.OnClickListener, interval: Long) {
    this.setOnClickListener(object : View.OnClickListener {
        private var mLastClick = 0L
        private var mInterval = max(0L, interval)
        override fun onClick(v: View) {
            val now = System.currentTimeMillis()
            if (now - mLastClick < mInterval) {
                return
            }
            mLastClick = now
            doClick(v)
        }

        private fun doClick(v: View) {
            listener.onClick(v)
        }
    })
}

/**
 * 忽略键盘显示隐藏
 * */
fun View.ignoreKeyBoard() {
    this.setTag(BaseActivity.IGNORE_KEYBOARD_STATUE, "IGNORE_KEYBOARD_STATUE")
}

/**
 * 约束布局相关
 * */
fun View.startToStartOf(@IdRes id: Int = PARENT_ID, doLayout: Boolean = true) {
    (this.layoutParams as? ConstraintLayout.LayoutParams)?.let {
        it.startToStart = id
        if (doLayout) {
            this.layoutParams = it
        }
    }
}

fun View.startToEndOf(@IdRes id: Int = PARENT_ID, doLayout: Boolean = true) {
    (this.layoutParams as? ConstraintLayout.LayoutParams)?.let {
        it.startToEnd = id
        if (doLayout) {
            this.layoutParams = it
        }
    }
}

fun View.endToStartOf(@IdRes id: Int = PARENT_ID, doLayout: Boolean = true) {
    (this.layoutParams as? ConstraintLayout.LayoutParams)?.let {
        it.endToStart = id
        if (doLayout) {
            this.layoutParams = it
        }
    }
}

fun View.endToEndOf(@IdRes id: Int = PARENT_ID, doLayout: Boolean = true) {
    (this.layoutParams as? ConstraintLayout.LayoutParams)?.let {
        it.endToEnd = id
        if (doLayout) {
            this.layoutParams = it
        }
    }
}

fun View.topToTopOf(@IdRes id: Int = PARENT_ID, doLayout: Boolean = true) {
    (this.layoutParams as? ConstraintLayout.LayoutParams)?.let {
        it.topToTop = id
        if (doLayout) {
            this.layoutParams = it
        }
    }
}

fun View.topToBottomOf(@IdRes id: Int = PARENT_ID, doLayout: Boolean = true) {
    (this.layoutParams as? ConstraintLayout.LayoutParams)?.let {
        it.topToBottom = id
        if (doLayout) {
            this.layoutParams = it
        }
    }
}

fun View.bottomToBottomOf(@IdRes id: Int = PARENT_ID, doLayout: Boolean = true) {
    (this.layoutParams as? ConstraintLayout.LayoutParams)?.let {
        it.bottomToBottom = id
        if (doLayout) {
            this.layoutParams = it
        }
    }
}

fun View.bottomToTopOf(@IdRes id: Int = PARENT_ID, doLayout: Boolean = true) {
    (this.layoutParams as? ConstraintLayout.LayoutParams)?.let {
        it.bottomToTop = id
        if (doLayout) {
            this.layoutParams = it
        }
    }
}


fun View.layoutGravity(gravity: Int) {
    (this.layoutParams as? FrameLayout.LayoutParams)?.let {
        val ly = it
        ly.gravity = gravity
        this.layoutParams = ly

        return@let
    }


    (this.layoutParams as? LinearLayout.LayoutParams)?.let {
        val ly = it
        ly.gravity = gravity
        this.layoutParams = ly

        return@let
    }
}


fun View.showSoft() {
    this.isFocusable = true
    this.isFocusableInTouchMode = true
    this.requestFocus()
    val imm =
        Utils.getApp().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager

    imm.showSoftInput(this, 0)
}

fun View.hideSoft() {
    val imm =
        Utils.getApp().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager

    imm.hideSoftInputFromWindow(this.windowToken, 0)
}